package com.wulijie.clone;


class Sheep1 implements Cloneable{
    private String name;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
/**
 * @Author cui
 * @Data
 */
public class Sheep{
    private String name;
    private int age;

    public Sheep1 sheep1; //浅拷贝克隆的是引用

    public Sheep(String name, int age) {
        this.name = name;
        this.age = age;
    }

//    @Override
//    public String toString() {
//        return "Sheep{" +
//                "name='" + name + '\'' +
//                ", age=" + age +
//                ", sheep1=" + sheep1 +
//                '}';
//    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Object clone() {
//        try (
//                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//                ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
//                ByteArrayInputStream byteArrayInputStream = null;
//                ObjectInputStream objectInputStream = null;
//        ) {
//            // 写入对象到输出流
//            objectOutputStream.writeObject(this);
//
//            // 重置输出流并创建输入流
//            byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
//            objectInputStream = new ObjectInputStream(byteArrayInputStream);
//
//            // 从输入流中读取对象
//            Object o = objectInputStream.readObject();
//            // 确保读取的对象是 Sheep 类型
//            if (o instanceof Sheep) {
//                return (Sheep) o;
//            } else {
//                throw new RuntimeException("Failed to clone Sheep object");
//            }
//        } catch (Exception e) {
//            throw new RuntimeException("Failed to clone Sheep object", e);
//        }
        return null;
    }

}

class test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Sheep sheep = new Sheep("zhangsan", 18);
        sheep.sheep1 = new Sheep1();
        System.err.println((Sheep) sheep.clone());
        System.err.println((Sheep) sheep.clone());
        System.err.println((Sheep) sheep.clone());


        System.err.println((Sheep1) ((Sheep) sheep.clone()).sheep1);
        System.err.println((Sheep1) ((Sheep) sheep.clone()).sheep1);
        System.err.println((Sheep1) ((Sheep) sheep.clone()).sheep1);


    }
}
